$(function () {
    loadFoot(0);
    share();
    var id=$.getUrlParam('did');
    //TODO:修改个人信息
    $.ajax({
        url:host+'/CMSAPI/GetUserCRM?id='+id+'&cid='+cid+ '&openid='+openid,
        type:'GET',
        success:function(data){
            if(data.state){
                var RealName=data.data.RealName||'';
                var addr=data.data.addr||'';
                var Phone=data.data.Phone||'';
                var proportion=data.data.proportion||'';
                var schedule=data.data.schedule||'';
                var style=data.data.style||'';
                var html='<li><i class="fa fa-address-book-o" aria-hidden="true"></i>&nbsp;&nbsp;姓名：'+RealName+'</li>'
                html+=Phone?'    <li><i class="fa fa-phone" aria-hidden="true"></i>&nbsp;&nbsp;电话：<a href="tel:'+Phone+'">'+Phone+'</a></li>':''
                html+=addr?'<li><i class="fa fa-map-marker" aria-hidden="true"></i>&nbsp;&nbsp;地址：'+addr+'</li>':''
                html+=proportion?'<li><i class="fa fa-cube" aria-hidden="true"></i>&nbsp;&nbsp;面积：'+proportion+'</li>':''
                html+=style?'<li><i class="fa fa-camera-retro" aria-hidden="true"></i>&nbsp;&nbsp;装修风格：'+style+'</li>':''
                html+=schedule?'<li><i class="fa fa-calendar" aria-hidden="true"></i>&nbsp;&nbsp;装修进度：'+schedule+'</li>':''
                $('.clientDetail').append(html);
            }
        }
    })
    //TODO:添加订单列表
    var index=0;
    var size=10;
    var message=0;
    $('section').dropload({
        scrollArea:window,
        distance:50,
        domUp:{
            domClass : 'dropload-up',
            domRefresh : '<div class="dropload-refresh">↓下拉刷新</div>',
            domUpdate : '<div class="dropload-update">↑释放更新</div>',
            domLoad : '<div class="dropload-load"><span class="loading"></span>加载中...</div>'
        },
        domDown:{
            domClass : 'dropload-down',
            domRefresh : '<div class="dropload-refresh">↑上拉加载更多</div>',
            domLoad : '<div class="dropload-load"><span class="loading"></span>加载中...</div>',
            domNoData : '<div class="dropload-noData">已经到底了，别扯了</div>'
        },
        loadDownFn:function(me){
            index++;
            message++;
            $.ajax({
                url:host+'/CMSAPI/GetUserAllOrder?'+'&cid='+cid+'&index='+index+'&size='+size+ '&openid='+id,
                type:'GET',
                dataType:'JSON',
                success:function(data){
                    console.log(data)
                    if(data.state){
                        if (data.data.length>0){
                            var html3='';
                            var list3=data.data;
                            $.each(list3,function(i,obj){
                                var RealName=obj.buy_name||'';
                                var productName=obj.name||'';
                                var orderState=obj.order_state||'';
                                var orderNum=obj.orderid||'';
                                var orderTime=obj.order_time||'';
                                var orderPrice=obj.total_price||'';
                                var orderCount=obj.count||'1';
                                var order_id=obj.order_id||'';
                                var dataId=obj.id;
                                html3+='<li data-id="'+order_id+'"><div class="detail"><div class="orderDetail"><div class="orderTitle overhide">'
                                html3+='    <p class="t1">'+productName+'</p>'
                                html3+='    <p class="t2">'+orderState+'</p>'
                                html3+='    </div><div class="orderState overhide">'
                                html3+='    <p class="t1">'+orderNum+'</p>'
                                html3+='    <p class="t2">'+orderTime+'</p>'
                                html3+='    </div></div><div class="overhide"><div class="clientName">'
                                html3+='     <span>'+RealName+'</span><br><span class="detailTip">点击列表查看订单详情</span>'
                                html3+='    </div><div class="priceTotal"><p>￥'+orderPrice+'</p>'
                                html3+='<p style="color: #aaa">×'+orderCount+'</p></div></div></div></li>'
                            });
                            $('.buyInfo').append(html3);
                        }else {
                            me.lock();
                            me.noData();
                        }
                    }else {
                        me.lock();
                        me.noData();
                        // alert('网络繁忙，请稍后重试！')
                    }
                    me.resetload();
                }
            })
        }
        // loadUpFn:function(me){
        //     location.reload();
        //     me.resetload();
        // }
    });
    //TODO:添加到访记录
    $.ajax({
        url:host+'/CMSAPI/GetVisitingList?id='+id+'&cid='+cid+'&index=1&size=100',
        success:function(data){
            if(data.state){
                var visitList=data.data.OrderList.reverse();
                var visitListLen=visitList.length;
                var html='';
                $.each(visitList,function(i,visit){
                    var time=visit.publishDate;
                    var t1=time.split(' ')[0]
                    var t2=time.split(' ')[1]
                    var aim=visit.summary||'';
                    var latest='';
                    if(Number(i)===Number(visitListLen-1)){
                        latest='latest'
                    }
                    html+='<li>'
                    html+='    <div class="time">'
                    html+='    <span>'+t2+'</span>'
                    html+='<span>'+t1+'</span>'
                    html+='</div>'
                    html+='<div class="info '+latest+'">'
                    html+='    <span>'+aim+'</span>'
                    html+='   </div>'
                    html+='    </li>'
                })
                $('.list').append(html);
            }
        }
    })
    //TODO:给订单列表添加点击事件(大图展示)
    var openPhotoSwipe = function(items) {
        var pswpElement = document.querySelectorAll('.pswp')[0];
        // build items array
        // define options (if needed)
        var options = {
            // history & focus options are disabled on CodePen
            history: true,
            focus: true,
            showAnimationDuration: 0,
            hideAnimationDuration: 0

        };
        var gallery = new PhotoSwipe( pswpElement, PhotoSwipeUI_Default, items, options);
        gallery.init();
    };
    $('.buyInfo').on('click','li',function(){
        var id=$(this).attr('data-id');
        $.ajax({
            url:host+'/CMSAPI/GetOrderDetail?id='+id,
            success:function(data){
                if(data.state){
                    var imgs=data.data.images;
                    if(imgs.length>0){
                        var photoSwiperItems=[];
                        $.each(imgs,function(i,img){
                            var url=img.url;
                            var w=img.width;
                            var h=img.height;
                            photoSwiperItems.push({src:url,w:w,h:h});
                        });
                        openPhotoSwipe(photoSwiperItems);
                    }else {
                        $.Dialog({
                            type:'alert',
                            content:'该订单没有图片！',
                            sureColor:'#6FA8EB'
                        })
                    }
                }
            }
        })
    })
});
